layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startSingleTime'
        	,value:new Date()
    });
    
    laydate.render({
    	elem:'#endSinglePutTime'
        	,value:new Date()
    });

    /**
     * 入库单表管理
     */
    var StoragePut = {
        tableId: "storagePutTable"
    };

    /**
     * 入库单明细表管理
     */
    var StoragePutSingle = {
        tableId: "storagePutSingleTable"
    };

    /**
     * 初始化表格的列
     */
    StoragePut.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'billstate', sort: true, title: 'Bill Status',templet: '#billstateTpl'},
            {field: 'customerName', sort: true, title: 'Customer Name'},
            {field: 'warehouseName', sort: true, title: 'Warehouse Name'},
            {field: 'storageNumber', sort: true, title: 'Receipt Number', style:'font-weight: 600;'},
            {field: 'putNumber', sort: true, title: 'Inbound Order Number'},
            {field: 'numall', sort: true, title: 'Expected Qty', style:'font-weight: 600;', totalRow: true},
            {field: 'realNumall', sort: true, title: 'Actual Qty', style:'font-weight: 600;', totalRow: true},
            {field: 'volumeall', sort: true, title: 'Volume', style:'font-weight: 600;', totalRow: true},
            {field: 'createTime', sort: true, title: 'Creation Time'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    StoragePut.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['billstate'] = $("#billstate").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['startSingleTime'] = $("#startSingleTime").val();
        queryData['endSinglePutTime'] = $("#endSinglePutTime").val();
        table.reload(StoragePut.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StoragePut.tableId,
        url: Feng.ctxPath + '/storagePut/querylist',
        page: true,
        limit: 40,
        height: 410,
        cellMinWidth: 100,
        toolbar: '#toolbarfrom',
		limits:[40,60,120,240,360,480,1200],
        totalRow: true ,//开启合计行
        cols: StoragePut.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        StoragePut.search();
    });

    // 单击行事件(点击入库单)
    table.on('row(' + StoragePut.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + StoragePut.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StoragePut.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
        storagePut(data);
    });

    /**
     * 初始化明细表格的列
     */
    StoragePutSingle.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'singleState', sort: true, title: 'Inbound Status',templet: '#singleStateTpl'},
            {field: 'stockId', sort: true, title: 'Stock ID', style:'font-weight: 600;', hide: true},
            {field: 'recommend', sort: true, title: 'Inbound Batch'},
            {field: 'positionNumber', merge: true, sort: true, title: 'Location', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: 'Tray', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'goodsname', sort: true, title: 'Name', style:'background-color: #dbf1e1;font-weight: 600;',width:270},
            {field: 'goodsNumber', sort: true, title: 'Number', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'Item Code', style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: 'Total:'},
            {field: 'putNum', merge: true, sort: true, title: 'Expected Qty', style:'background-color: #dbf1e1;font-weight: 600;', totalRow: true},
            {field: 'realNum', merge: true, sort: true, title: 'Actual Qty', style:'background-color: #dbf1e1;font-weight: 600;', totalRow: true},
            {field: 'singleDateofmanufacture', sort: true, title: 'Manufacturing Date', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'singleBatchnumber', sort: true, title: 'Batch', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'putWeight', title: 'Weight', totalRow: true},
            {field: 'detailedPutvolume', title: 'Volume', totalRow: true},
            {field: 'unitprice', title: 'Unit Price'},
            {field: 'singleMoney', title: 'Amount', totalRow: true},
            {field: 'singleLargeclass', title: 'Category'},
            {field: 'singleGoodstype', title: 'Type', hide: true},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'singlePacking', title: 'Packing'},
            {field: 'goodsSpecs', title: 'Specs'},
            {align: 'center', toolbar: '#tableBarSingle', title: 'Operation',fixed: 'right'}
        ]];
    };

    // 单击行事件(点击入库单)
    table.on('row(' + StoragePutSingle.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + StoragePutSingle.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StoragePutSingle.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    });
    
    function storagePut(data){
    	var storageNumber;
    	if (data!=null) {
    		$("#storageNumber").val(data.storageNumber);
    		storageNumber = data.storageNumber;
		} else {
			storageNumber = 'nodata';
		}
		
        /**
         * 点击查询按钮
         */
    	StoragePutSingle.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition2").val();
            table.reload(StoragePutSingle.tableId, {
                where: queryData, page: {curr: 1},
                 done:function () {
                     // 搜索按钮点击事件
                     $('#btnSearchDetailed').click(function () {
                    	 StoragePutSingle.search();
                     });
                }
            });
        };
        
        // 渲染商品明细表格
        var tableResult = table.render({
            elem: '#' + StoragePutSingle.tableId,
            url: Feng.ctxPath + '/storagePutSingle/querylist?storageNumber=' + $("#storageNumber").val(),
            page: true,
            limit: 50,
            height: 400,
            toolbar: '#toolbarfromSingle',
            limits: [10,20,30,40,50,60,80,100,500,1000,5000,10000],
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: StoragePutSingle.initColumn()
        });

        // 搜索按钮点击事件
        $('#btnSearchDetailed').click(function () {
        	StoragePutSingle.search();
        });

        // 打印按钮点击事件
        $('#print').click(function () {
            layer.open({
                    type: 2,
                    title: 'Inbound Receipt Detail Print',
                    area: ['750px', '600px'],
                    anim: 5,
                    content: Feng.ctxPath + '/storagePutSingle/print',
                    end: function () {
                        storagePut(data);
                    }
                });
        });

        /**
         * 点击批量打印托盘按钮
         */
    	StoragePutSingle.btnPrint = function () {
	    	var checkStatus = table.checkStatus('storagePutSingleTable');
	        data = checkStatus.data;
	        if (data.length>0) {
	            var ids = [];
	            for (var i = 0; i < data.length; i++) {
	            	ids.push(data[i].id);
				}
		        layer.open({
	            type: 2,
	            title: 'Batch Print Tray Codes',
	            area: ['900px', '550px'],
	            anim: 5,
	            content: Feng.ctxPath + '/storagePutSingle/tobtnqrCode?ids='+ids,
	            end: function () {
	            }
	        });
			} else {
				layer.alert('Please select at least one record');
			}
        };
        
	    $('#btnPrint').click(function () {
	    	StoragePutSingle.btnPrint();
	    });

        // 工具条点击事件
        table.on('tool(' + StoragePutSingle.tableId + ')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'yichangchulijilu') {
                layer.open({
                    type: 2,
                    title: 'Exception Handling Records',
                    area: ['1000px', '500px'],
                    anim: 5,
                    content: Feng.ctxPath + '/storagePutException?detailedId=' + data.id,
                });
            }
        });
    }
    
    
    
});
